<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10 paddingTop0">
      <div class="main_ti">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="标准库" name="1"></el-tab-pane>
          <el-tab-pane label="非标准库" name="2"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="mainor_form">
        <div class="mainor_te">
          <!-- class="mainor_tea" -->
          <el-button @click="exportb" v-repeatClick>导入</el-button>
        </div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="关键字" prop="name">
            <el-input
              v-model.trim="form.name"
              placeholder="名称"
              clearable
              class="wid220"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="category_id" class="wid220">
            <el-cascader
              v-model="form.category_id"
              :options="options"
              :props="defaultProps"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="导入情况" prop="is_export">
            <el-select
              v-model.trim="form.is_export"
              placeholder="导入情况"
              class="wid220"
            >
              <el-option label="所有" value=""></el-option>
              <el-option label="未导入" value="1"></el-option>
              <el-option label="已导入" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              class="reset marginLeft10"
              type="primary"
              @click="$refs['userListFrom'].resetFields()"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="main_table" style="min-height: 620px" v-if="activeName == 1">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.list.data"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :row-class-name="getRowClass"
          @selection-change="handleSelectionChange"
          :header-cell-class-name="this.$verticaline"
        >
          <el-table-column type="expand" class-name="main_hei">
            <template slot-scope="props">
              <el-table
                :data="props.row.spu"
                class="mainex_ta"
                fit
                :highlight-current-row="false"
              >
                <el-table-column label width="40" align="right" />
                <el-table-column
                  label="SKU规格"
                  prop="spu_name.title"
                  width="200"
                >
                  <template slot-scope="scope">
                    {{ scope.row.spu_name ? scope.row.spu_name.title : "" }}
                  </template>
                </el-table-column>
                <el-table-column label="单位" prop="spu_unit.title" width="200">
                  <template slot-scope="scope">
                    {{ scope.row.spu_unit ? scope.row.spu_unit.title : "" }}
                  </template>
                </el-table-column>
                <el-table-column label="互调比" prop="package_num" />
                <el-table-column label prop width="80" />
              </el-table>
            </template>
          </el-table-column>

          <el-table-column type="selection" />
          <el-table-column label="序号">
            <template slot-scope="scope">{{
              (current.page - 1) * current.per_page + scope.$index + 1
            }}</template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop="name"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="商品图片" width="120" class-name="main_hei">
            <template slot-scope="scope">
              <div class="demo-image__error mainor_im">
                <el-image
                  fit="fill"
                  :src="scope.row.pic"
                  :preview-src-list="scope.row.pics"
                >
                  <div slot="error" class="image-slot">
                    <img :src="puzzleImg" />
                  </div>
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="分类"
            prop="userNickname"
            width="180"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope"
              >{{ scope.row.category_p ? scope.row.category_p.title : "" }}|{{
                scope.row.category ? scope.row.category.title : ""
              }}</template
            >
          </el-table-column>
          <el-table-column
            label="编码"
            prop="standard_code"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="导入次数"
            prop="tally"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="管理操作"
            align="center"
            fixed="right"
            width="150"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <router-link :to="'/commodity/gallery/see/1/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <el-link
                :type="scope.row.goods == null ? 'warning' : 'info'"
                @click="
                  (e) => {
                    scope.row.goods == null
                      ? galleryImport(e, scope.row.id)
                      : '';
                  }
                "
                v-repeatClick
                >导入</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_table" style="min-height: 610px" v-else>
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.list.data"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :row-class-name="getRowClass"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="expand" class-name="main_hei">
            <template slot-scope="props">
              <el-table
                :data="props.row.spu"
                class="mainex_ta"
                fit
                :highlight-current-row="false"
              >
                <el-table-column label width="40" align="right" />
                <el-table-column
                  label="SKU规格"
                  prop="spu_name.title"
                  width="200"
                >
                  <template slot-scope="scope">
                    {{ scope.row.spu_name ? scope.row.spu_name.title : "" }}
                  </template>
                </el-table-column>
                <el-table-column label="单位" prop="spu_unit.title" width="200">
                  <template slot-scope="scope">
                    {{ scope.row.spu_unit ? scope.row.spu_unit.title : "" }}
                  </template></el-table-column
                >
                <el-table-column label="互调比" prop="package_num" />
                <el-table-column label prop width="80" />
              </el-table>
            </template>
          </el-table-column>

          <el-table-column type="selection" />
          <el-table-column label="序号">
            <template slot-scope="scope">{{
              (current.page - 1) * current.per_page + scope.$index + 1
            }}</template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop="name"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="商品图片" width="120" class-name="main_hei">
            <template slot-scope="scope">
              <div class="demo-image__error mainor_im">
                <el-image
                  fit="fill"
                  :src="scope.row.pic"
                  :preview-src-list="scope.row.pics"
                >
                  <div slot="error" class="image-slot">
                    <img :src="puzzleImg" />
                  </div>
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="分类"
            prop="userNickname"
            width="180"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope"
              >{{ scope.row.category_p ? scope.row.category_p.title : "" }}|{{
                scope.row.category ? scope.row.category.title : ""
              }}</template
            >
          </el-table-column>
          <el-table-column
            label="编码"
            prop="goods_code"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="导入次数"
            prop="tally"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="管理操作"
            align="center"
            fixed="right"
            width="150"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <router-link :to="'/commodity/gallery/see/2/' + scope.row.id">
                <el-link type="success">查看</el-link>
              </router-link>
              <el-link
                :type="scope.row.goods == null ? 'warning' : 'info'"
                @click="
                  (e) => {
                    scope.row.goods == null
                      ? galleryImport(e, scope.row.id)
                      : '';
                  }
                "
                v-repeatClick
                >导入</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  standard_index,
  nonstandard_index,
  standard_import,
  nonstandard_import,
  standard_batchImport,
  nonstandard_batchImport,
} from "@/api/commodity/commodityGallery";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category } from "@/utils/index";
import { puzzleImg } from "@/api/currency";

export default {
  computed: {
    ...mapGetters(["avatar"]),
  },
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 搜索
      form: {
        name: "",
        pid: "",
        category_id: [],
        is_export: null,
      },
      // 更多
      moredata: false,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      puzzleImg,
      listLoading: false,
      // 全选
      selectArr: [],
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
      // 选中菜单
      activeName: "1",
    };
  },
  methods: {
    // 选中菜单
    handleClick(tab, event) {
      this.form = {
        name: "",
        pid: "",
        category_id: [],
        is_export: null,
      };
      this.current = {
        page: 1,
        per_page: 20,
      };
      this.limit = 20;
      this.$refs["userListFrom"].clearValidate();
      if (tab.name == 1) {
        this.standard_index();
      } else {
        this.nonstandard_index();
      }
    },
    // 批量导入
    exportb() {
      let selectArr = JSON.parse(JSON.stringify(this.selectArr));
      let arry = [];
      selectArr.map((e) => {
        if (e.goods == null) {
          arry.push({ id: e.id });
        }
      });
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { ids: JSON.stringify(arry) }
      );
      if (selectArr.length != 0) {
        if (arry.length != 0) {
          if (this.activeName == "1") {
            standard_batchImport(data).then((res) => {
              if (res.errcode == 0) {
                this.$message({
                  message: "导入成功",
                  type: "success",
                });
                this.$refs.multipleTable.clearSelection();
                this.standard_index();
              } else {
                this.$message.error(res.errmsg);
              }
            });
          } else {
            nonstandard_batchImport(data).then((res) => {
              if (res.errcode == 0) {
                this.$message({
                  message: "导入成功",
                  type: "success",
                });
                this.$refs.multipleTable.clearSelection();
                this.nonstandard_index();
              } else {
                this.$message.error(res.errmsg);
              }
            });
          }
        } else {
          this.$message.error("不能导入商品");
        }
      } else {
        this.$message.error("请勾选商品");
      }
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id, goods: item.goods });
      });
    },
    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      if (row.row.spu) {
        if (row.row.spu.length === 0) {
          return "row-expand-cover";
        }
      }
    },
    // 导入
    galleryImport(e, id) {
      if (e.path[1].disabled) {
        return;
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      if (this.activeName == "1") {
        standard_import(data).then((res) => {
          if (res.errcode == 0) {
            this.$message({
              message: "导入成功",
              type: "success",
            });
            this.standard_index();
          } else {
            this.$message.error(res.errmsg);
          }
        });
      } else {
        nonstandard_import(data).then((res) => {
          if (res.errcode == 0) {
            this.$message({
              message: "导入成功",
              type: "success",
            });
            this.nonstandard_index();
          } else {
            this.$message.error(res.errmsg);
          }
        });
      }
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      if (this.activeName == "1") {
        this.standard_index();
      } else {
        this.nonstandard_index();
      }
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      if (this.activeName == "1") {
        this.standard_index();
      } else {
        this.nonstandard_index();
      }
    },
    changePage(page) {
      this.current.page = page;
      if (this.activeName == "1") {
        this.standard_index();
      } else {
        this.nonstandard_index();
      }
    },
    // 列表
    standard_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      standard_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.categoryList.map((e) => {
            e.selfNext = e.self_next;
            if (e.self_next) {
              e.self_next.map((a) => {
                a.selfNext = [];
              });
            }
          });
          console.log(res.data.categoryList);
          this.options = recursion(res.data.categoryList, true);
          console.log(this.options);
          res.data.list.data.map((e) => {
            e.pic = e.pic ? this.avatar.domain + e.pic : "";
            e.pics = e.pics
              ? e.pics.split(",").map((p) => this.avatar.domain + p)
              : [];
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    nonstandard_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      nonstandard_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.categoryList.map((e) => {
            e.selfNext = e.self_next;
            if (e.self_next) {
              e.self_next.map((a) => {
                a.selfNext = [];
              });
            }
          });
          this.options = recursion(res.data.categoryList, true);
          res.data.list.data.map((e) => {
            e.pic = e.pic ? this.avatar.domain + e.pic : "";
            e.pics = e.pics
              ? e.pics.split(",").map((p) => this.avatar.domain + p)
              : [];
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);
      return form;
    },
  },
  activated() {
    // 列表
    if (this.activeName == 1) {
      this.standard_index();
    } else {
      this.nonstandard_index();
    }
  },
  created() {},
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainor_form form {
  width: calc(100% - 60px);
}
.maincl {
  .main_table /deep/.el-table--small td {
    padding: 10px 0;
  }
  .main_table /deep/.el-table--small th {
    padding: 10px 0;
  }

  /deep/.el-table {
    tr {
      &:hover {
        background: none !important;
      }
      &.hover-row > td {
        background: none !important;
      }
      .current-row > td {
        background: none !important;
      }
    }
  }
  /deep/.el-table .el-table__body tr:hover {
    background: #fff !important;
  }
  /deep/.el-table .el-table__body tr:hover > td {
    background: #fff !important;
  }
  /deep/.el-table tr.expanded + tr {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr > td {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr .mainex_ta {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr .mainex_ta th,
  /deep/.el-table tr.expanded + tr .mainex_ta tr {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr:hover {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr:hover > td {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr .el-table tr:hover {
    background: #fafafa !important;
  }
  /deep/.el-table tr.expanded + tr .el-table tr:hover > td {
    background: #fafafa !important;
  }
}

/deep/.el-tabs__item:focus.is-active.is-focus:not(:active) {
  box-shadow: none !important;
}
</style>
